package realtime.history;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 处理五分钟历史数据，补数操作
 * @author lit
 * @date 2018/4/18.
 */
public class Main {
    /**
     * 处理历史数据必须要传入两个时间字符串作为参数，而且按时间大小由小到大顺序传入
     * @param args 传入两个时间字符串作为参数输入参数格式为yyyyMMddHHmm，
     *             eg1:'201804180525' '201804180545' process half one hour trip data
     *             eg2:'201804180000' '201804200000' process two day trip data
     */
    public static void main(String[] args){
        try{
            if(args == null || args.length <= 0){
                return;
            }else{
                Logger logger = LoggerFactory.getLogger(realtime.statistic.Main.class);
                logger.info("****************即将处理实时统计历史数据:{}-{}******************",args[0],args[1]);
                //五分钟循环
                String startPrefix = args[0].substring(0,10);
                int minute = 5 * (Integer.parseInt(args[0].substring(10,12))/5);
                String start = startPrefix + String.format("%02d",minute);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                     ProcessHistoryTrip pt = new ProcessHistoryTrip();
                ProcessHistoryTripIndex pti = new ProcessHistoryTripIndex();
                Timestamp startTime = new Timestamp(sdf.parse(start).getTime());
                Timestamp endTime = new Timestamp(sdf.parse(args[1]).getTime());

                long s = System.currentTimeMillis();
                String temp;
                while(startTime.getTime() < endTime.getTime()){
                    startTime = new Timestamp(startTime.getTime() + 5*60*1000);
                    temp = sdf.format(startTime);
                    logger.info("开始处理的时间点是{}前5分钟历史统计", temp);
                    String statTime = null;
                    try {
                        statTime = sdf1.format(sdf.parse(temp).getTime());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    String lastStatMonth = getRealtimeStatTime(statTime);
                    pt.processTrip(lastStatMonth,statTime);
                    pti.processTripIndex(lastStatMonth,statTime);
                }
                long end = System.currentTimeMillis();
                logger.info("耗时：{}s",(end - s)/1000);
                pt.finishMSSQL();
                pti.finishMSSQL();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void processRealtimeHistory(String datetime){

    }

    /**
     * 获取上一个统计月份，如现在4月，那上一个统计月份是3月
     * @return eg:'2018-03-01 00:00:00'
     * @param statTime 统计时间 eg:'2018-04-17 10:20:00'
     */
    private static String getRealtimeStatTime(String statTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(statTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int year = calendar.get(Calendar.YEAR);
        //Calendar.MONTH默认值范围是0-11
        int month = calendar.get(Calendar.MONTH);

        calendar.set(year,month,1,0,0,0);
        calendar.add(Calendar.MONTH,-1);
        return sdf.format(calendar.getTime());
    }
}
